package com.yds.ydsdatasetbackend.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yds.ydsdatasetbackend.annotation.AuthCheck;
import com.yds.ydsdatasetbackend.common.BaseResponse;
import com.yds.ydsdatasetbackend.common.ResultUtils;
import com.yds.ydsdatasetbackend.constant.UserConstant;
import com.yds.ydsdatasetbackend.exception.BusinessException;
import com.yds.ydsdatasetbackend.exception.ErrorCode;
import com.yds.ydsdatasetbackend.exception.ThrowUtils;
import com.yds.ydsdatasetbackend.model.dto.file.FileUpdateRequest;
import com.yds.ydsdatasetbackend.model.dto.file.variables.FileVariablesQueryRequest;
import com.yds.ydsdatasetbackend.model.dto.file.variables.FileVariablesUpdateRequest;
import com.yds.ydsdatasetbackend.model.dto.user.UserQueryRequest;
import com.yds.ydsdatasetbackend.model.entity.File;
import com.yds.ydsdatasetbackend.model.entity.FileVariables;
import com.yds.ydsdatasetbackend.model.entity.User;
import com.yds.ydsdatasetbackend.model.vo.FileVariablesVO;
import com.yds.ydsdatasetbackend.model.vo.UserVO;
import com.yds.ydsdatasetbackend.service.FileVariablesService;
import com.yds.ydsdatasetbackend.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/file/variables")
public class FileVariablesController {

    @Resource
    private FileVariablesService fileVariablesService;

    @Resource
    private UserService userService;


    /**
     * 更新图片（仅管理员可用）
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateFileVariables(@RequestBody FileVariablesUpdateRequest fileVariablesUpdateRequest, HttpServletRequest request) {
        if (fileVariablesUpdateRequest == null || fileVariablesUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 将实体类和 DTO 进行转换
        FileVariables fileVariables = new FileVariables();
        BeanUtils.copyProperties(fileVariablesUpdateRequest, fileVariables);

        User loginUser = userService.getLoginUser(request);

        // 数据校验,角色校验
        fileVariablesService.validFileVariable(fileVariables, loginUser);
        // 判断是否存在
        long id = fileVariablesUpdateRequest.getId();
        FileVariables oldFileVariables = fileVariablesService.getById(id);
        ThrowUtils.throwIf(oldFileVariables == null, ErrorCode.NOT_FOUND_ERROR);
        // 操作数据库
        boolean result = fileVariablesService.updateById(fileVariables);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }


    /**
     * 根据 id 获取用户（仅管理员）
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<FileVariables> getFileVariablesById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        FileVariables fileVariables = fileVariablesService.getById(id);
        ThrowUtils.throwIf(fileVariables == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(fileVariables);
    }

    /**
     * 根据 id 获取包装类
     */
    @GetMapping("/get/vo")
    public BaseResponse<FileVariablesVO> getFileVariablesVOById(long id) {
        BaseResponse<FileVariables> response = getFileVariablesById(id);
        FileVariables fileVariables = response.getData();
        return ResultUtils.success(fileVariablesService.getFileVariablesVO(fileVariables));
    }

    /**
     * 分页获取用户封装列表（管理员）
     * @param fileVariablesQueryRequest
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<FileVariablesVO>> listFileVariablesVOByPage(@RequestBody FileVariablesQueryRequest fileVariablesQueryRequest) {
        ThrowUtils.throwIf(fileVariablesQueryRequest == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(fileVariablesQueryRequest.getFileId() == null, ErrorCode.PARAMS_ERROR, "请传入对应的文件id");
        long current = fileVariablesQueryRequest.getCurrent();
        long pageSize = fileVariablesQueryRequest.getPageSize();
        Page<FileVariables> fileVariablesPage = fileVariablesService.page(new Page<>(current, pageSize), fileVariablesService.getQueryWrapper(fileVariablesQueryRequest));
        Page<FileVariablesVO> fileVariablesVOPage = new Page<>(current, pageSize, fileVariablesPage.getTotal());
        List<FileVariablesVO> fileVariablesVOList = fileVariablesService.getFileVariablesVOList(fileVariablesPage.getRecords());
        fileVariablesVOPage.setRecords(fileVariablesVOList);
        return ResultUtils.success(fileVariablesVOPage);
    }

}
